package com.xia.service;

import com.xia.dao.AuthRoleFunctionMapper;
import com.xia.dao.AuthRoleMapper;
import com.xia.dao.AuthUserRoleMapper;
import com.xia.entity.*;
import com.xia.utils.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleService {

    private final AuthRoleMapper roleMapper;
    private final AuthRoleFunctionMapper roleFunctionMapper;
    private final AuthUserRoleMapper authUserRoleMapper;

    @Autowired(required = false)
    public RoleService(AuthRoleMapper roleMapper, AuthRoleFunctionMapper roleFunctionMapper, AuthUserRoleMapper authUserRoleMapper) {
        this.roleMapper = roleMapper;
        this.roleFunctionMapper = roleFunctionMapper;
        this.authUserRoleMapper = authUserRoleMapper;
    }

    @Transactional
    public void addRole(AuthRole role, List<AuthRoleFunction> roleFunctions) {
        role.setId(IDUtils.genItemId());
        this.roleMapper.insert(role);
        for (AuthRoleFunction roleFunction : roleFunctions) {
            roleFunction.setRoleId(role.getId());
            roleFunction.setId(IDUtils.genItemId());
            this.roleFunctionMapper.insert(roleFunction);
        }
    }

    @Transactional
    public void editRole(AuthRole role, List<AuthRoleFunction> roleFunctions) {
        this.roleMapper.updateByPrimaryKeySelective(role);

        AuthRoleFunctionExample example = new AuthRoleFunctionExample();
        AuthRoleFunctionExample.Criteria cri = example.createCriteria();
        cri.andRoleIdEqualTo(role.getId());
        this.roleFunctionMapper.deleteByExample(example);

        for (AuthRoleFunction rf : roleFunctions) {
            rf.setRoleId(role.getId());
            rf.setId(IDUtils.genItemId());
            this.roleFunctionMapper.insert(rf);
        }
    }

    @Transactional
    public void deleteRole(Long roleId) {
        this.roleMapper.deleteByPrimaryKey(roleId);

        AuthRoleFunctionExample roleFunctionExample = new AuthRoleFunctionExample();
        AuthRoleFunctionExample.Criteria cri1 = roleFunctionExample.createCriteria();
        cri1.andRoleIdEqualTo(roleId);
        this.roleFunctionMapper.deleteByExample(roleFunctionExample);

        AuthUserRoleExample userRoleExample = new AuthUserRoleExample();
        AuthUserRoleExample.Criteria cri2 = userRoleExample.createCriteria();
        cri2.andRoleIdEqualTo(roleId);
        this.authUserRoleMapper.deleteByExample(userRoleExample);
    }

    public List<AuthRole> getRoles() {
        List<AuthRole> roleList = this.roleMapper.selectByExample(new AuthRoleExample());
        roleList.forEach(authRole -> {
            authRole.setRoleFunctionList(
                    getRoleFunction(authRole.getId())
            );
        });
        return roleList;
    }

    public List<AuthRole> getRoles(List<Long> ids) {
        AuthRoleExample example = new AuthRoleExample();
        AuthRoleExample.Criteria cri = example.createCriteria();
        cri.andIdIn(ids);
        List<AuthRole> roleList = this.roleMapper.selectByExample(example);
        roleList.forEach(authRole -> {
            authRole.setRoleFunctionList(
                    getRoleFunction(authRole.getId())
            );
        });
        return roleList;
    }


    public List<AuthRoleFunction> getRoleFunction(Long roleId) {
        AuthRoleFunctionExample roleFunctionExample = new AuthRoleFunctionExample();
        AuthRoleFunctionExample.Criteria cri = roleFunctionExample.createCriteria();
        cri.andRoleIdEqualTo(roleId);
        return this.roleFunctionMapper.selectByExample(roleFunctionExample);
    }

}
